package com.derbysoft.lowcostadapter.dswitch3.service.impl;

import com.derbysoft.lowcostadapter.commons.exception.HotelNotFoundException;
import com.derbysoft.lowcostadapter.commons.model.Hotel;
import com.derbysoft.lowcostadapter.commons.model.Status;
import com.derbysoft.lowcostadapter.commons.redis.service.HotelCacheService;
import com.derbysoft.lowcostadapter.commons.repository.HotelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

/**
 * Created by: jason
 * Date: 2012-05-15
 */
public abstract class BaseHotelService {

    public static final String SYSTEM = "System";

    @Value("${provider}")
    protected String provider;

    @Value("${dswitch3.adapter}")
    protected String adapter;

    @Autowired
    protected HotelCacheService hotelCacheService;


    @Autowired
    protected HotelRepository hotelRepository;

    protected Hotel loadHotel(String propertyId) {
        Hotel hotel = hotelCacheService.loadById(propertyId);

        if (hotel == null) {
            hotel = hotelRepository.findByPropertyID(Integer.parseInt(propertyId), Status.ENABLED);
            if (hotel == null) {
                throw new HotelNotFoundException(propertyId);
            }
        }
        return hotel;
    }

    public void setHotelCacheService(HotelCacheService hotelCacheService) {
        this.hotelCacheService = hotelCacheService;
    }

    public void setHotelRepository(HotelRepository hotelRepository) {
        this.hotelRepository = hotelRepository;
    }
}
